﻿using System;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace lcqwp
{
    /// <summary>
    /// Extension methods for PublishingWeb objects
    /// </summary>
    [CLSCompliant(false)]
    public static class PublishingWebExtensions
    {
        /// <summary>
        /// Retirves the current culture of the SPWeb the extension 
        /// is called on. If the SPWeb object is not a publishing web 
        /// or it is not part of a variation, the current culture of 
        /// the thread is returned.
        /// </summary>
        /// <param name="site">the site for which to find the culture</param>
        /// <returns>the culture of the site or of the thread</returns>
        public static CultureInfo GetCurrentCulture(this SPWeb site)
        {
            CultureInfo cultureInfo = CultureInfo.CurrentCulture;

            if (PublishingWeb.IsPublishingWeb(site))
            {
                PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(site);
                VariationLabel variationLabel = pubWeb.Label;
                if (variationLabel != null)
                {
                    cultureInfo = new CultureInfo(variationLabel.Language);
                }
            }
            return cultureInfo;
        }
    }
}
